package com.cdream.basic.java8.model;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.RandomUtil;
import lombok.SneakyThrows;

import java.util.Date;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;

public class Shop {
    public static String getPrice(String product) {
        double price = calculatePrice(product);
        Discount.Code code = Discount.Code.values()[RandomUtil.randomInt(Discount.Code.values().length)];
        return String.format("%s:%.2f:%s", code.name(), price, code.percentage);
    }

    public static Future<Double> getPriceAsync(String product) {
        CompletableFuture<Double> futurePrice = new CompletableFuture<>();
        new Thread(() -> {
            double res = calculatePrice(product);
            futurePrice.complete(res);
        }).start();
        return futurePrice;
    }

    private static double calculatePrice(String product) {
        ThreadUtil.sleep(1000);
        return RandomUtil.randomDouble(100) * product.charAt(0) + product.charAt(1);
    }

    @SneakyThrows
    public static void main(String[] args) {
        System.out.println(getPriceAsync("dog").get());
        System.out.println(getPrice("aog"));
        System.out.println(DateUtil.offset(new Date(), DateField.MONTH, 2));
        System.out.println(DateUtil.thisDayOfMonth());
    }
}
